#include "TextParser.h"

AssetDataList & TextParser::VLoad(Resource & res, char * bfr, unsigned int sze )
{
	file_ptr = 0; size = sze;  source = bfr; text_mode = false;
	char buffer[128];
	VBegin();
	while(file_ptr != size)
	{
		ParseNext(buffer);
		Stack.ProcessString(buffer);
	}
	VEnd();
	return VGetData();
}


void TextParser::ParseNext( char * param )
{
	int i = 0;
	while((!IsEndChar(source[file_ptr]) || text_mode) && file_ptr != size)
	{
		param[i] = source[file_ptr];
		if(param[i] == '"')
		{
			text_mode = !text_mode;
		}
		++i;
		++file_ptr;
	}
	param[i] = '\0';
	SkipFreeSpace();
}
void TextParser::PreParseNext( char * param )
{
	int i = 0;
	while((!IsEndChar(source[file_ptr + i]) || text_mode) && file_ptr != size)
	{
		param[i] = source[file_ptr + i];
		if(param[i] == '"')
		{
			text_mode = !text_mode;
		}
		++i;
	}
	param[i] = '\0';
}
void TextParser::ParseNext( )
{
	while((!IsEndChar(source[file_ptr]) || text_mode) && file_ptr != size)
	{
		if(source[file_ptr] == '"')
		{
			text_mode = !text_mode;
		}
		++file_ptr;
	}
	SkipFreeSpace();
}
void TextParser::SkipFreeSpace()
{
	while(IsEndChar(source[file_ptr]) && file_ptr != size)
	{
		++file_ptr;
	}
}

bool TextParser::IsLetter(const char n)
{
	if((n >= 'a' && n <= 'z' ) || ( n >= 'A' && n <= 'Z' ))
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool TextParser::IsNumber(const char n)
{
	if(n >= '0' && n <= '9')
	{
		return true;
	}else{
		return false;
	}
}
bool TextParser::IsInteger(const char * const n)
{
	int i = 0;
	while(n[i] != '\0')
	{
		if(!IsNumber(n[i]))
		{
			return false;
		}
		++i;
	}
	return true;
}
bool TextParser::IsFloat(const char * const n)
{
	int i = 0;
	while(n[i] != '\0')
	{
		if(!(IsNumber(n[i]) || n[i] == '.'))
		{
			return false;
		}
		++i;
	}
	if(i <= 8)
	{
		return true;
	}else
	{
		return false;
	}
}
bool TextParser::IsDouble(const char * const n)
{
	int i = 0;
	while(n[i] != '\0')
	{
		if(!(IsNumber(n[i]) || n[i] == '.'))
		{
			return false;
		}
		++i;
	}
	if(i <= 8)
	{
		return false;
	}else
	{
		return true;
	}
}
bool TextParser::IsCapital(const char n)
{
	if(n >= 'A' && n <= 'Z')
	{
		return true;
	}else{
		return false;
	}
}
bool TextParser::IsEndChar(const char n)
{
	if(IsFreeSpace(n) || n == '\r')
	{
		return true;
	}else
	{
		return false;
	}
}

bool TextParser::IsFreeSpace(const char n)
{
	if(n == ' ' || n == char(9) || n == '\n')
	{
		return true;
	}else{
		return false;
	}
}


bool AreEqual(const char * const a, const char * const b) // really fast cmp function
{
	unsigned int l = strlen(a), l2 = strlen(b);
	if(l != l2)
	{
		return false;
	}
	if(a[0] != b[0])
	{
		return false;
	}
	--l2;
	if(a[l2] != a[l2])
	{
		return false;
	}
	l2 = l / 2;
	if(a[l2] != b[l2])
	{
		return false;
	}
	l2 = l / 3;
	if(a[l2] != b[l2])
	{
		return false;
	}
	return true;
}

int TextParser::CharToInt(const char * const n)
{
	char i = 0; int sum = 0; char sign = 1;
	if(n[i] == '-')
	{
		sign = -1;
		++i;
	}
	while(n[i] != '\0')
	{
		sum *= 10;
		sum += n[i];
		++i;
	}
	return sum * sign;
}
